package utils;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateUtil
{
	// The sessionFactory object
	private static SessionFactory sessionFactory;

	// Disables instantiation of HibernateUtil constructor
	private HibernateUtil()
	{
	}

	// Needed for clients to get the sessionFactory object
	public static SessionFactory getSessionFactory()
	{
		// Initialize sessionFactory on first call
		if (sessionFactory == null)
		{
			Configuration configuration = new Configuration().configure();
			ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
					.applySettings(configuration.getProperties())
					.buildServiceRegistry();
			sessionFactory = configuration.buildSessionFactory(serviceRegistry);
		}
		return sessionFactory;
	}
}